<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>tree</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }

        .tree {
            margin: 50px;
            line-height: 30px;
        }

        .tree-line > .tree-line {
            padding-left: 40px;
            display: none;
        }

        .tree-line.expand > .tree-line {
            display: block;
        }

        .tree-line.expand > .title > .tree-expand {
            display: none;
        }

        .tree-line:not(.expand) > .title > .tree-shrink {
            display: none;
        }


    </style>
</head>
<body>
    <div class="tree">
        <!-- 点击出现折叠效果，需要一个状态标识 expand -->
        <!-- <div class="tree-line expand">
            <div class="title">
                <span class="tree-expand">+</span>
                <span class="tree-shrink">-</span>
                家用电器
            </div>
    
            <div class="tree-line">
                <div class="title">
                    <span class="tree-expand">+</span>
                    <span class="tree-shrink">-</span>
                    大家电
                </div>
    
                <div class="tree-line">
                    <div class="title">
                        <span class="tree-expand">+</span>
                        <span class="tree-shrink">-</span>
                        空调
                    </div>
    
                    <div class="tree-line">海尔空调</div>
                    <div class="tree-line">美的空调</div>
                </div>
            </div>
        </div> -->
    </div>

    <script src="../lib/data/treeData.js"></script>
    <script src="../lib/jquery/jquery.min.js"></script>
    <script type="text/javascript">
        console.log(arr);

        function dealTree(treeArr){
            var str = "";
    
            for(var i = 0; i < treeArr.length; i ++){
                if(treeArr[i].children){
                    str += `<div class="tree-line expand">
                        <div class="title">
                            <span class="tree-expand">+</span>
                            <span class="tree-shrink">-</span>
                            ${ treeArr[i].name }
                        </div>
        
                        ${ dealTree(treeArr[i].children) }
                    </div>`;
                }else {
                    str += `<div class="tree-line">${ treeArr[i].name }</div>`;
                }
            }

            return str;
        }
        
        $(".tree").html(dealTree(arr));
        
        $(".tree").on("click", ".title", function (){
            // 切换父元素的类名 expand
            $(this).parent().toggleClass("expand");
        })
    </script>
</body>
</html>